#include "filter.h"
#include <iostream>
#include <sstream>

Filter::Filter(){
    data = NULL;
}

Filter::Filter(Data* _data) : data(_data){
}

Filter::~Filter(){
}

Data *Filter::getData(){
    return data;
}

unsigned char Filter::normalize(double x0, double xn, double y0, double yn, double x){
    int i = ((x - x0) * (yn - y0)) / (xn - x0) + y0;

    return i;
}

double Filter::normalize2(double x0, double xn, double y0, double yn, double x){
    double i = ((x - x0) * (yn - y0)) / (xn - x0) + y0;

    return i;
}

//double Filter::distance(int x1, int y1, int x2, int y2){
//    return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);//como  so para comparação não está sendo usado a raiz quadrada
//}

Data *Filter::padding(int up, int right, Data *_data, int bottom, int left){
    if(!_data) _data = data;

    if (right == -1){
        if (up == 0) return _data;
        right = bottom = left = up;
    }else if (bottom == -1){
        if (up == 0 && right == 0) return _data;
        bottom = up;
        left = right;
    }else if (left == -1){
        if (up == 0 && right == 0 && bottom == 0) return _data;
        left = right;
    }

    Data *newData = new Data(_data->getWidth() + right + left, _data->getHeight() + up + bottom);

    for(int i = 0; i < _data->getWidth() +1; i++){
        for(int j = 0; j < _data->getHeight() +1; j++){
            newData->setData(i + left, j + up, _data->getData(i, j));
        }
    }
//    delete _data;
    _data = newData;

    return _data;
}

Data *Filter::cropping(int up, int right, Data *_data, int bottom, int left){
    if(!_data) _data = data;

    if (right == -1){
        if (up == 0) return _data;
        right = bottom = left = up;
    }else if (bottom == -1){
        if (up == 0 && right == 0) return _data;
        bottom = up;
        left = right;
    }else if (left == -1){
        if (up == 0 && right == 0 && bottom == 0) return _data;
        left = right;
    }

    Data *newData = new Data(_data->getWidth() - right - left, _data->getHeight() - up - bottom);

    for(int i = 0; i < newData->getWidth(); i++){
        for(int j = 0; j < newData->getHeight(); j++){
            newData->setData(i, j, _data->getData(i + left, j + up));
        }
    }

//    delete _data;
    _data = newData;

    return _data;
}



std::string intToStr(int _i){
    std::stringstream out;
    out << _i;
    return out.str();
}

std::string doubleToStr(double _i){
    std::stringstream out;
    out << _i;
    return out.str();
}
